package com.futureCloudMOM.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.futureCloudMOM.common.exception.base.BaseException;
import com.futureCloudMOM.core.base.service.impl.BaseServiceImpl;
import com.futureCloudMOM.core.domain.*;
import com.futureCloudMOM.core.dto.WmsInventoryMainDTO;
import com.futureCloudMOM.core.mapper.*;
import com.futureCloudMOM.core.service.WmsInventoryMainService;
import com.futureCloudMOM.core.utils.UUIDUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
* 【WMS_InventoryMain】的数据库操作Service实现
*
* @author Administrator
* @since 2023-06-07 09:14:43
*/
@Service
@AllArgsConstructor
public class WmsInventoryMainServiceImpl extends BaseServiceImpl<WmsInventoryMainMapper, WmsInventoryMain> implements WmsInventoryMainService{

    // 盘点子表
    private final WmsInventorySubMapper wmsInventorySubMapper;
    // 盘点库位
    private final WmsInventoryAreaMapper wmsInventoryAreaMapper;
    // 盘点物料
    private final WmsInventoryMaterialMapper wmsInventoryMaterialMapper;
    // 盘点条码初期
    private final WmsInventoryPeriodMapper wmsInventoryPeriodMapper;
    // 条码库位
    private final WmsBarCodeQuanMapper wmsBarCodeQuanMapper;
    // 条码
    private final WmsBarCodeInfoMapper wmsBarCodeInfoMapper;

    @Override
    @Transactional
    public void created(WmsInventoryMainDTO wmsInventoryMainDTO) {
        // 盘点单号
        wmsInventoryMainDTO.setBillNo(UUIDUtils.createdUUID("PD"));
        wmsInventoryMainDTO.setStatus(0L);
        // 新增盘点单
        save(wmsInventoryMainDTO);
        // 盘点库位新增
        wmsInventoryMainDTO.getAreaList().forEach(wmsInventoryAreaMapper::insert);
        // 盘点物料新增
        wmsInventoryMainDTO.getMaterialLsit().forEach(wmsInventoryMaterialMapper::insert);
    }

    @Override
    @Transactional
    public void startInventory(Long id) {
        // 根据id查询盘点单信息
        WmsInventoryMain wmsInventoryMain = getById(id);
        if(wmsInventoryMain.getStatus() != 1){
            throw new BaseException("状态为新建的盘点单才可以执行此操作");
        }
        // 盘点单的物料和库位信息
        List<WmsInventoryMaterial> materialList = wmsInventoryMaterialMapper.selectList(new LambdaQueryWrapper<WmsInventoryMaterial>()
        .eq(WmsInventoryMaterial::getMainId,id)
        .eq(WmsInventoryMaterial::getDeleted,0));
        // 查询状态为盘点中，仓库和要进行盘点的仓库相同的盘点单
        List<WmsInventoryMain> mainList = list(new LambdaQueryWrapper<WmsInventoryMain>()
        .eq(WmsInventoryMain::getStatus,1)
        .eq(WmsInventoryMain::getWHId,wmsInventoryMain.getWHId())
        .eq(WmsInventoryMain::getDeleted,0));
        // 判断有没有相同仓库 相同库位 相同物料的盘点单在进行中
        mainList.forEach(item -> {
            // 查询盘点中的库位和物料信息
            List<WmsInventoryMaterial> materials = wmsInventoryMaterialMapper.selectList(new LambdaQueryWrapper<WmsInventoryMaterial>()
                    .eq(WmsInventoryMaterial::getMainId,item.getId())
                    .eq(WmsInventoryMaterial::getDeleted,0));
            materialList.forEach(el -> {
                // 找出库位和物料一致的信息
                List<WmsInventoryMaterial> list = materials.stream().filter(material -> (material.getAreaId().equals(el.getAreaId()) && material.getMaterialId().equals(el.getMaterialId()))).collect(Collectors.toList());
                if(list.size() > 0){
                    //StringBuilder stringBuilder = new StringBuilder();
                    //list.forEach(main -> stringBuilder.append("库位：【").append(main.getAreaId()).append("】，物料：【").append(main.getMaterialId()).append("】"));
                    //stringBuilder.append("正在盘点中");
                    //throw new BaseException(stringBuilder.toString());
                    throw new BaseException("有同库位同物料的盘点单正在盘点");
                }
            });
        });
        // 将要盘点的所有物料的条码状态改为盘点锁定
        // 根据仓库ID 库位ID 物料ID查询条码
        materialList.forEach(item -> {
            List<WmsBarCodeQuan> wmsBarCodeQuanList = wmsBarCodeQuanMapper.selectList(new LambdaQueryWrapper<WmsBarCodeQuan>()
            .eq(WmsBarCodeQuan::getWHID,wmsInventoryMain.getWHId())
            .eq(WmsBarCodeQuan::getAreaID,item.getAreaId())
            .eq(WmsBarCodeQuan::getMaterialID,item.getMaterialId())
            .eq(WmsBarCodeQuan::getDeleted,0));
            wmsBarCodeQuanList.forEach(el ->{
                // 根据条码号查询条码
                WmsBarCodeInfo wmsBarCodeInfo = wmsBarCodeInfoMapper.selectOne(new LambdaQueryWrapper<WmsBarCodeInfo>()
                .eq(WmsBarCodeInfo::getBarCode,el.getBarCode()));
                wmsBarCodeInfo.setStatus(2L);
                wmsBarCodeInfoMapper.updateById(wmsBarCodeInfo);
                // 条码信息保存到条码初期表
                WmsInventoryPeriod wmsInventoryPeriod = new WmsInventoryPeriod();
                wmsInventoryPeriod.setMainId(id);
                wmsInventoryPeriod.setBarcode(el.getBarCode());
                wmsInventoryPeriod.setQty(wmsBarCodeInfo.getQuantity());
                wmsInventoryPeriod.setWHID(wmsInventoryMain.getWHId());
                wmsInventoryPeriod.setAreaId(item.getAreaId());
                wmsInventoryPeriod.setMaterialId(item.getMaterialId());
                wmsInventoryPeriodMapper.insert(wmsInventoryPeriod);
            });
        });
        // 更新盘点主表状态为盘点中
        wmsInventoryMain.setStatus(1L);
        updateById(wmsInventoryMain);
    }

    @Override
    @Transactional
    public void submit(List<WmsInventorySub> list) {
        // 将盘点的实际信息保存到盘点子表
        list.forEach(wmsInventorySubMapper::insert);
        WmsInventoryMain wmsInventoryMain = getById(list.get(0).getMainId());
        // 更新状态为盘点完成
        wmsInventoryMain.setStatus(2L);
        updateById(wmsInventoryMain);
    }

}
